package com.ctrip.ec.biz.dao.module;
import com.ctrip.ec.dal.DaoBase;
import com.ctrip.ec.biz.entity.module.ModuleRoleEntity;
import com.ctrip.ec.dal.DbAccess;
import com.ctrip.ec.dal.IdConvert;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

@Service
public class ModuleRoleDao extends DaoBase<ModuleRoleEntity> {
    public ModuleRoleDao() {
        super(ModuleRoleEntity.class);
    }
    public ModuleRoleDao(DbAccess db) {
        super(ModuleRoleEntity.class,db);
    }

    public List<Long> listByRoleId(Long roleId) throws SQLException {
        String sql = "select module_id from " + this.getTableName() + " where role_id =?";
        return db.query(Long.class,sql,roleId);
    }

    public void saveByRoleId(List<Long> moduleIds, Long roleId) throws SQLException {
        if(moduleIds!= null && moduleIds.size()>0){
            String sql = "delete from " + this.getTableName() + " where role_id = ?";
            this.db.execute(sql,roleId);

            for(Long mid :moduleIds){
                ModuleRoleEntity entity = new ModuleRoleEntity();
                entity.setModuleId(mid);
                entity.setRoleId(roleId);
                this.insert(entity);
            }
        }

    }
}
